package org.yaukie.util;

import java.io.File;
import java.io.FileFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ClassUtil {
    private static final Logger log = LoggerFactory.getLogger(ClassUtil.class);

    public ClassUtil() {
    }

    public static ClassLoader getClassLoader() {
        return Thread.currentThread().getContextClassLoader();
    }

    public static Class<?> loadClass(String className, boolean isInitialized) {
        Class cls = null;

        try {
            cls = Class.forName(className, isInitialized, getClassLoader());
        } catch (ClassNotFoundException var4) {
            if (log.isErrorEnabled()) {
                log.error("load class error .......");
                throw new RuntimeException(var4);
            }
        }

        return cls;
    }

    public static File[] getClassFiles(String packagePath) {
        File[] files = (new File(packagePath)).listFiles(new FileFilter() {
            public boolean accept(File file) {
                return file.isFile() && file.getName().endsWith(".class") || file.isDirectory();
            }
        });
        return files;
    }

    public static String getClassName(String packageName, String fileName) {
        String className = fileName.substring(0, fileName.lastIndexOf("."));
        if (StringUtil.isNotEmpty(packageName)) {
            className = packageName + "." + className;
        }

        return className;
    }

    public static String getSubPackagePath(String packagePath, String filePath) {
        String subPackagePath = filePath;
        if (StringUtil.isNotEmpty(packagePath)) {
            subPackagePath = packagePath + "/" + filePath;
        }

        return subPackagePath;
    }

    public static String getSubPackageName(String packageName, String filePath) {
        String subPackageName = filePath;
        if (StringUtil.isNotEmpty(packageName)) {
            subPackageName = packageName + "." + filePath;
        }

        return subPackageName;
    }
}
